﻿using CBS.APP;
using CBS.Desktop.FormPage;
using CBS.Model.ManageModel;
using CBS.Model.UIModel;
using CBS.Model.ViewModel;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CBS.Desktop.ViewPartial
{
    /// <summary>
    /// TeacherSemesterAmount.xaml 的交互逻辑
    /// </summary>
    public partial class TeacherSemesterAmount : UserControl
    {
        public Pagination pager = new Pagination() { rows = 10, page = 1, sidx = nameof(TeacherSemesterAmountViewModel.SemesterAmountId), sord = "desc" };

        public TeacherSemesterAmount()
        {
            InitializeComponent();
            LoadCondation();
        }

        private async void LoadCondation()
        {
            btn_Search.IsEnabled = false;

            // 加载 ComboBox 数据
            await LoadDataAsync();

            // 查询 Grid 数据
            FlushGrid();

            btn_Search.IsEnabled = true;
        }

        private async Task LoadDataAsync()
        {
            // 使用 Task.WhenAll 等待所有 ComboBox 数据加载完成
            var tasks = new List<Task>
            {
                BindSemester(),
            };

            await Task.WhenAll(tasks);
        }

        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // 调用处理方法
                pager.page = 1;
                pg_DataList.PageIndex = 1;
                FlushGrid();
                e.Handled = true; // 如果需要，标记事件已处理
            }
        }
        private async Task BindSemester()
        {
            try
            {
                btn_Search.IsEnabled = false;
                var list = await Task.Run(() => SemesterApp.GetSelectList());

                if (list.Count == 0)
                {
                    return;
                }


                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Semester.ItemsSource = list;
                }));
                cb_Semester.SelectedIndex = 0;

                FlushGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Window.GetWindow(this), ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async void FlushGrid()
        {
            try
            {
                btn_Search.IsEnabled = false;

                int semesterId = ((SemesterSelectModel)cb_Semester.SelectedItem).SemesterId;
                string key = tb_SearchKey.Text.Trim();

                var data = await Task.Run(() => TeacherApp.GetTeacherAmountSemesterList(semesterId, key, pager));

                this.Dispatcher.Invoke(() =>
                {
                    pg_DataList.MaxPageCount = pager.total;
                    dgr_DataList.ItemsSource = data;
                    btn_Search.IsEnabled = true;
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show(Window.GetWindow(this), ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private void pg_DataList_PageUpdated(object sender, HandyControl.Data.FunctionEventArgs<int> e)
        {
            pager.page = e.Info;
            FlushGrid();
        }

        private void dg_Edit(object sender, RoutedEventArgs e)
        {
            var tmp = sender as System.Windows.Controls.Button;
            var data = tmp.DataContext as TeacherSemesterAmountViewModel;
            EditTeacherSemesterAmount student = new EditTeacherSemesterAmount(data.SemesterAmountId);
            student.Owner = Window.GetWindow(this);
            var dialogResult = student.ShowDialog();
            if (dialogResult.HasValue && dialogResult.Value)
            {
                FlushGrid();
            }
        }
    }
}
